﻿using Farming.Domain;
using Farming.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Linq.Expressions;

namespace Farming.Repositories
{
    public class Repository<T>:IRepository<T> where T:class
    {
        private readonly FarmingDbContext db;
        public Repository(FarmingDbContext db)
        {
            this.db = db;
        }
        //class 标识是引用类型
        /// <summary>
        /// 单条添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="e"></param>
        /// <returns></returns>
        public virtual async Task<int> InSertAsync(T e)
        {
            await db.Set<T>().AddAsync(e);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public virtual async Task<int> InsertAsync(List<T> e)
        {
            await db.Set<T>().AddRangeAsync(e);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public virtual async Task<int> DeleteAsync(int id)
        {
            var entity= await db.Set<T>().FindAsync(id);
            db.Set<T>().Remove(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 根据条件删除
        /// </summary>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        public virtual async Task<int> DeleteAsync(Expression<Func<T,bool>> expression)
        {
            var entity = await db.Set<T>().Where(expression).ToListAsync();
            db.Set<T>().RemoveRange(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 根据主键查找
        /// </summary>
        /// <param name="id">主键Id</param>
        /// <returns></returns>
        public virtual async Task<T>GetByIdAsync(int id)
        {
            return await db.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<T>> GetAllAsync()
        {
            return await db.Set<T>().ToListAsync();
        }
        /// <summary>
        /// 单条修改
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public virtual async Task<int> UpdateAsync(T entity)
        {
             db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities">类</param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(List<T> entity)
        {
            db.Set<T>().UpdateRange(entity);
            return await db.SaveChangesAsync();
        }

        /// <summary>
        /// 根据条件查找
        /// </summary>
        /// <param name="expression">拉姆达表达式</param>
        /// <returns></returns>
        public async Task<T> FindAsync(Expression<Func<T, bool>> expression)
        {
            return await db.Set<T>().Where(expression).FirstOrDefaultAsync();
        }
        /// <summary>
        /// 根据条件查找
        /// </summary>
        /// <returns></returns>
        public virtual async Task<List<T>> GetAllByEx(Expression<Func<T,bool>> ex)
        {
            return await db.Set<T>().Where(ex).ToListAsync();
        }

    }
}
